package basicprocess.authentication;

import java.util.Hashtable;

import com.sun.org.apache.regexp.internal.recompile;

import query.view.Condition;
import utility.CapitalChar;
import utility.ConvertToLower;
import utility.Input;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;



public class Report implements basicprocess.authentication.Authentication
{
	public basicprocess.Authentication authentication;
	String object;
	Hashtable<Object, Object> table =new Hashtable<Object, Object>();
	RecordSearch sr;
	String access;
	String ACCESS="access";
	DBManager database;
	GetIdName gid;
	String CONDITION="condition";
	String REPORT="report";
	
	public boolean getResult()
	{	
		try 
		{
			initializeData();
			type.Report typeofreport=new type.Report(database,table.get(REPORT).toString());
			String typereport = typeofreport.GettypeofReport();
			Hashtable<Object, Object> newreportmap =typeofreport.getReportMap();
			query.condition.typereport.Typereport type; 
			Class C = Class.forName("query.condition.typereport."+CapitalChar.makeFirstCharCapital(typereport.trim()));
			type = (query.condition.typereport.Typereport)C.newInstance();
			type.setData("","", database, newreportmap);
			String path = type.getPath(gid.getItem(table.get(REPORT).toString()));
			query.view.Condition condition =new Condition();
			condition.setObject(table.get(CONDITION).toString());
			condition.setDbmanager(database);
			condition.getResult();
			String request1stchar = authentication.pool.getRequestParameter().getProcess().substring(0, 1).toLowerCase();
			access = access.toLowerCase();
			if(condition.getbuffer().length>0)
			{
				if(access.contains(request1stchar))
					return false;
				else
					return true;
			}	
			else
				return true;
		} 
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		}
		catch (InstantiationException e) 
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e) 
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public void initializeData() 
	{
		table.clear();
		String MasterPath = Input.MASTERREPORTSPATH + object +"//" + object + "_mid" + Input.FILRFORMAT;
		table.put("mid", object);
		table.put("td", "null");
		sr.setConditionMap(table);
		String line[] = sr.getArrayFromResultSet();
		table.clear();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			String value = splitline[Column.pv_index-1];
			if(value.equals("0"))
					value =  splitline[Column.vt_index-1];
				table.put(splitline[Column.pid_index-1], value);
		}
		table = ConvertToLower.convertHashKey(table);
		if(table.containsKey(ACCESS))
		{
			access = table.get(ACCESS).toString();
			access = gid.getItem(access);
			if(access.equals("no$"))
				access = table.get(ACCESS).toString();
		}	
	}
	
	public void setobject(Object object, String obj) 
	{
		authentication = ( basicprocess.Authentication) object;
		this.database = authentication.database;
		this.gid = new GetIdName(database);
		CONDITION = gid.getId(CONDITION);
		REPORT = gid.getId(REPORT);
		this.object = obj;
		ACCESS = gid.getId(ACCESS);
	}

}
